package cmd

import (
	"AccessAdmin/internal/controller"
	"AccessAdmin/internal/controller/frontend"
	"AccessAdmin/internal/service"
	"AccessAdmin/utility/response"
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/util/gmode"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			var (
				s   = g.Server()
				oai = s.GetOpenApi()
			)

			// OpenApi自定义信息
			oai.Info.Title = `API Reference`
			//oai.Config.CommonResponse = response.JsonRes{}
			//oai.Config.CommonResponseDataField = `Data`

			//s.Use(middleware.ErrorHandler)

			// 静态目录设置
			uploadPath := g.Cfg().MustGet(ctx, "setting.uploadDir").String()
			if uploadPath == "" {
				return gerror.NewCode(gcode.CodeInvalidConfiguration, "文件上传配置路径不能为空")
			}
			s.AddStaticPath("/resource", uploadPath)

			// HOOK, 开发阶段禁止浏览器缓存,方便调试
			if gmode.IsDevelop() {
				s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
					r.Response.Header().Set("Cache-Control", "no-store")
				})
			}

			// 前台系统自定义错误页面
			s.BindStatusHandler(401, func(r *ghttp.Request) {
				if response.IsRestfulRequest(r) {
					response.JsonExit(r, 401, "您尚未登录，请登录后操作")
				} else {
					service.View().Render401(r.Context())
				}
			})

			s.BindStatusHandler(403, func(r *ghttp.Request) {
				if response.IsRestfulRequest(r) {
					response.JsonExit(r, 403, "您无权访问此接口，请稍后重试")
				} else {
					service.View().Render403(r.Context())
				}
			})

			s.BindStatusHandler(404, func(r *ghttp.Request) {
				if response.IsRestfulRequest(r) {
					response.JsonExit(r, 404, "非法的接口，请勿重复尝试，系统已自动记录Ip")
				} else {
					service.View().Render404(r.Context())
				}
			})

			s.BindStatusHandler(500, func(r *ghttp.Request) {
				if response.IsRestfulRequest(r) {
					response.Error(r)
				} else {
					service.View().Render500(r.Context())
				}
			})

			s.BindHandler("/", func(r *ghttp.Request) {
				r.Response.WriteExit("SUCCESS")
			})

			// 后台前缀
			s.Group("/admin", func(group *ghttp.RouterGroup) {

				group.Middleware(
					service.Middleware().Ctx,
					service.Middleware().ResponseHandler,
				)

				group.Bind(
					controller.Login,
					controller.Captcha,
				)

				// 权限控制路由
				group.Group("/", func(group *ghttp.RouterGroup) {
					group.Middleware(service.Middleware().Auth)
					group.Bind(
						controller.Index,
						controller.User,
						controller.Role,
						controller.Menu,
						controller.LoginLog,
						controller.Attachment,
						controller.AttachmentCate,
					)
				})
			})

			// 启动前台项目gtoken
			frontendToken, err := StartFrontendGToken()
			if err != nil {
				return err
			}

			//前台项目路由组
			s.Group("/api", func(group *ghttp.RouterGroup) {
				group.Middleware(
					service.Middleware().CORS,
					service.Middleware().Ctx,
					service.Middleware().ResponseHandler,
				)
				//需要登录鉴权的路由组
				group.Group("/", func(group *ghttp.RouterGroup) {
					err := frontendToken.Middleware(ctx, group)
					if err != nil {
						return
					}
					//需要登录鉴权的接口放到这里
					group.Bind(
						frontend.User,
					)
				})
			})

			s.Run()
			return nil
		},
	}
)
